package editors.objectedit;
import java.util.Vector;

import physics.Body;
import tools.Help;
import tools.Point;
import game.objects.GameObject;
import graphics.objects.GraphicsObject;
public class BoundingBox {
	public float minX;
	public float minY;
	public float maxY;
	public float maxX;
	public Point center;
	public BoundingBox(GameObject o) {
		minX = Float.POSITIVE_INFINITY;
		minY = Float.POSITIVE_INFINITY;
		maxX = Float.NEGATIVE_INFINITY;
		maxY = Float.NEGATIVE_INFINITY;
		for (int i = 0; i < o.numBodies; i++) {
			Body b = o.getBody(i);
			if (b.pos.x - b.width < minX)
				minX = b.pos.x - b.width;
			if (b.pos.x + b.width > maxX)
				maxX = b.pos.x + b.width;
			if (b.pos.y - b.height < minY)
				minY = b.pos.y - b.height;
			if (b.pos.y + b.height > maxY)
				maxY = b.pos.y + b.height;
		}
		for (int i = 0; i < o.numGraphicsObjects; i++) {
			GraphicsObject g = o.getGraphicsObject(i);
			if (g.node.pos.x - g.node.boundingrad < minX)
				minX = g.node.pos.x - g.node.boundingrad;
			if (g.node.pos.x + g.node.boundingrad > maxX)
				maxX = g.node.pos.x + g.node.boundingrad;
			if (g.node.pos.y - g.node.boundingrad < minY)
				minY = g.node.pos.y - g.node.boundingrad;
			if (g.node.pos.y + g.node.boundingrad > maxY)
				maxY = g.node.pos.y + g.node.boundingrad;
		}
		// Enforce quadratic shape:
		// if ((maxY - minY) < (maxX - minX)){
		// maxY += (maxX - minX) - (maxY - minY);
		// }
		// else{
		// maxX += (maxY - minY) - (maxX - minX);
		// }
		construct();
	}
	public BoundingBox(Vector<Body> bl, Vector<GraphicsObject> gl) {
		minX = Float.POSITIVE_INFINITY;
		minY = Float.POSITIVE_INFINITY;
		maxX = Float.NEGATIVE_INFINITY;
		maxY = Float.NEGATIVE_INFINITY;
		for (int i = 0; i < bl.size(); i++) {
			Body b = bl.get(i);
			if (b.pos.x - b.width < minX)
				minX = b.pos.x - b.width;
			if (b.pos.x + b.width > maxX)
				maxX = b.pos.x + b.width;
			if (b.pos.y - b.height < minY)
				minY = b.pos.y - b.height;
			if (b.pos.y + b.height > maxY)
				maxY = b.pos.y + b.height;
		}
		for (int i = 0; i < gl.size(); i++) {
			GraphicsObject g = gl.get(i);
			if (g.node.pos.x - g.node.boundingrad < minX)
				minX = g.node.pos.x - g.node.boundingrad;
			if (g.node.pos.x + g.node.boundingrad > maxX)
				maxX = g.node.pos.x + g.node.boundingrad;
			if (g.node.pos.y - g.node.boundingrad < minY)
				minY = g.node.pos.y - g.node.boundingrad;
			if (g.node.pos.y + g.node.boundingrad > maxY)
				maxY = g.node.pos.y + g.node.boundingrad;
		}
		construct();
	}
	public BoundingBox(float minX, float minY, float maxX, float maxY) {
		this.minX = minX;
		this.minY = minY;
		this.maxY = maxY;
		this.maxX = maxX;
		construct();
	}
	public BoundingBox move(Point d) {
		return new BoundingBox(minX + d.x, minY + d.y, maxX + d.x, maxY + d.y);
	}
	private void construct() {
		center = new Point((minX + maxX) * 0.5f, (minY + maxY) * 0.5f);
	}
	@Override
	public String toString() {
		return minX + " " + minY + " " + maxX + " " + maxY;
	}
	public Point calcCenter() {
		return new Point((minX + maxX) * 0.5f, (minY + maxY) * 0.5f);
	}
}
